package foo.bar.wiki.web.action.user;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.extension.UserTypeConverter;
import foo.bar.wiki.services.UserService;
import foo.bar.wiki.domain.User;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationMethod;
import net.sourceforge.stripes.validation.SimpleError;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class RemoveUserActionBean extends BaseActionBean {

    private UserService userService;

    @Validate(converter= UserTypeConverter.class)
    private User user;


    @DefaultHandler
    public Resolution remove() {
        userService.deleteUser(user);
        getContext().getMessages().add(new SimpleMessage("User ["+user.getUsername()+"] has been deleted"));
        return new RedirectResolution(ListUsersActionBean.class, "view");
    }
    @ValidationMethod(on={"remove"})
    public void validate_remove() {
        if (user == null || user.getId() == null) {
            getContext().getValidationErrors().addGlobalError(new SimpleError("User ["+user.getUsername()+"] with id ["+user.getId()+"] doesn't exists"));
        }
    }


    
    @SpringBean("userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
